using Godot;

public class EditViewportInteractive : Control
{


	public EditViewport EditViewport { get; internal set; }


	/*const string INPUT_MOUSE_LEFT = "ui_canvas_select";
	const string INPUT_MOUSE_MIDDLE = "ui_canvas_move";
	const string INPUT_MOUSE_RIGHT = "ui_canvas_select";
	const string INPUT_ZOOM_IN = "ui_canvas_zoom_in";
	const string INPUT_ZOOM_OUT = "ui_canvas_zoom_out";

	[Export]
	private Color _rulerBGColor;
	[Export]
	private Color _rulerLineColor;
	[Export]
	private StyleBox _preselectStyleBox;
	[Export]
	private Color _selectedBoxColor;

	private CanvasMover _canvasMover;
	private TransformBox _transformBox;

	public override void _Ready()
	{
		if (_preselectStyleBox == null) _preselectStyleBox = GetStylebox("Box", "Selecter");
		if (_preselectStyleBox == null) _preselectStyleBox = new StyleBoxFlat()
		{
			BgColor = Color.FromHsv(210f / 360f, 1, 1, 0.45f),
			BorderWidthBottom = 1,
			BorderWidthLeft = 1,
			BorderWidthRight = 1,
			BorderWidthTop = 1,
			BorderColor = Color.FromHsv(210f / 360f, 1, 1)
		};
		_canvasMover = new CanvasMover(Update, _root);
		_transformBox = new TransformBox();
	}


	private string _debugInfo;
	private OperatorModeType GetCursorShape(Vector2 point, Rect2 rect)
	{
		rect.Position -= new Vector2(4, 4);
		rect.Size += new Vector2(8, 8);
		if (rect.HasPoint(point))
		{
			point -= rect.Position;
			var size = rect.Size;
			_debugInfo = $"{point} , {size} , {size - point}";
			if (point.x <= 8)
				if (point.y <= 8)
					return OperatorModeType.Fdiagsize;
				else if (size.y - point.y <= 8)
					return OperatorModeType.Bdiagsize;
				else
					return OperatorModeType.Hsize;
			else if (size.x - point.x <= 8)
				if (point.y <= 8)
					return OperatorModeType.Bdiagsize;
				else if (size.y - point.y <= 8)
					return OperatorModeType.Fdiagsize;
				else
					return OperatorModeType.Hsize;
			else if (point.y <= 8 || size.y - point.y <= 8)
				return OperatorModeType.Vsize;
			else
				return OperatorModeType.Move;
		}
		else
			return OperatorModeType.SingleSelect;
	}

	private void OnInputEventMouse(InputEventMouse e)
	{
		CanvasOperator(e);
		_transformBox.OnMouseEvent(e);
		if (_transformBox.HasTargets)
		{
			var point = e.Position;
			var op = GetCursorShape(point, _transformBox.Rect);
			MouseDefaultCursorShape = (CursorShape)op;
			if (op != OperatorModeType.SingleSelect)
			{
				if (e.IsActionPressed(INPUT_MOUSE_LEFT))
					_transformBox.Bein(e, op);
				//_selecter.BeginMove(e, op);
				else if (e.IsActionReleased(INPUT_MOUSE_LEFT))
					_transformBox.End(e, op);
				//_selecter.EndMove(e, op);
				return;
			}
		}
		*//*if (e.IsActionPressed(INPUT_MOUSE_LEFT))
			_selecter.Begin(e);
		else if (e.IsActionReleased(INPUT_MOUSE_LEFT))
			_transformBox.SetTargets(RectGlobalPosition, _canvasMover.Scale, _selecter.End(e));*//*
	}
	public override void _GuiInput(InputEvent e)
	{
		if (e is InputEventMouse m)
		{
			OnInputEventMouse(m);
		}
	}
	private void CanvasOperator(InputEventMouse e)
	{
		_canvasMover.MoveTo(e.Position);
		if (e.IsActionReleased(INPUT_ZOOM_IN))
			_canvasMover.ZoomIn();
		else if (e.IsActionReleased(INPUT_ZOOM_OUT))
			_canvasMover.ZoomOut();
		if (e.IsActionPressed(INPUT_MOUSE_MIDDLE))
			_canvasMover.Begin(e.Position);
		else if (e.IsActionReleased(INPUT_MOUSE_MIDDLE))
			_canvasMover.End();
	}

	// Called when the node enters the scene tree for the first time.
	public override void _Draw()
	{
		base._Draw();
		*//*if (_selecter.OperatorMode == OperatorModeType.MultipleSelect)
			DrawStyleBox(_preselectStyleBox, _selecter.PreselectRect);*//*
		if (_transformBox.HasTargets)
		{
			var selectedRect = _transformBox.Rect;
			const float size = 8;
			var (xMin, yMin, xMax, yMax) = selectedRect.GetMinMax();
			DrawRect(selectedRect, _selectedBoxColor, false);
			DrawPoint(xMin, yMin, size, _selectedBoxColor, false);
			DrawPoint(xMax, yMin, size, _selectedBoxColor, false);
			DrawPoint(xMin, yMax, size, _selectedBoxColor, false);
			DrawPoint(xMax, yMax, size, _selectedBoxColor, false);
			DrawPoint((xMin + xMax) * 0.5f, yMin, size, _selectedBoxColor, false);
			DrawPoint((xMin + xMax) * 0.5f, yMax, size, _selectedBoxColor, false);
			DrawPoint(xMin, (yMin + yMax) * 0.5f, size, _selectedBoxColor, false);
			DrawPoint(xMax, (yMin + yMax) * 0.5f, size, _selectedBoxColor, false);
			DrawPoint((xMin + xMax) * 0.5f, (yMin + yMax) * 0.5f, size, _selectedBoxColor, false);
		}
		var fount = GetThemeDefaultFont();
		DrawString(fount, new Vector2(0, 16), $"{_canvasMover.Scale:P0}");
		//DrawString(fount, new Vector2(16, 48), _debugInfo);
		DrawRect(new Rect2(0, -16, RectSize.x, 16), _rulerBGColor);
		DrawRect(new Rect2(-16, 0, 16, RectSize.y), _rulerBGColor);
		DrawRect(new Rect2(-16, -16, 16, 16), _rulerLineColor);
		DrawRect(new Rect2(-15, -16, RectSize.x + 15, RectSize.y + 15), _rulerLineColor, false);
	}

	private void DrawPoint(Vector2 point, float size, Color color, bool isCircle)
	{
		if (isCircle)
			DrawCircle(point, size, color);
		else
			DrawRect(point.ToRectFromCentre(size), color);
	}
	private void DrawPoint(float x, float y, float size, Color color, bool filled)
	{
		DrawPoint(new Vector2(x, y), size, color, filled);
	}*/

}
